package NISO;

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D; 
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.font.TextAttribute;
import java.text.AttributedString;
 
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.Timer;

 

public class Panel extends JPanel implements ActionListener {
	JLabel mode, username, password, adv;
	String[] modes = {"Owner", "Manager", "Bookkeeper"};
	JComboBox box;
	JButton ok, cancel;
	JTextField usernme;
	JPasswordField pass;
	LogFrame log;
	Link advanced = new Link(100, 210, 92, 110, 100, 102, 310, 185);
	String user, passw, userType;//->>>the input of the users is stored here:)
	String mysqluser, mysqlpass, mysqlhost;//->> input of the advanced settings
	int port; //->> input of the advanced settings
	boolean logframe;
	
	public Panel(){
		setFocusable(true);
		this.addKeyListener(new ML());
		this.addMouseListener(new ML());
		logframe=false;
		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		
		this.setBorder(BorderFactory.createEmptyBorder(8, 10, 10, 10));//creates a space before the mode selection :)
		
		JPanel modePanel = new JPanel();
		modePanel.setLayout(new BoxLayout(modePanel, BoxLayout.LINE_AXIS));
		JPanel userPanel = new JPanel();
		userPanel.setLayout(new BoxLayout(userPanel, BoxLayout.LINE_AXIS));
		JPanel passPanel = new JPanel();
		passPanel.setLayout(new BoxLayout(passPanel, BoxLayout.LINE_AXIS));
		JPanel advPanel = new JPanel();
		advPanel.setLayout(new BoxLayout(advPanel, BoxLayout.LINE_AXIS));
		JPanel okPanel = new JPanel();
		okPanel.setLayout(new BoxLayout(okPanel, BoxLayout.LINE_AXIS));
		 
		mode     = new JLabel("         Mode:             ");
		username = new JLabel("       Username:      ");
		password = new JLabel("         Password:      ");
		adv 	 = new JLabel(" ");
		box      = new JComboBox(modes);
		ok       = new JButton("Log In");
		cancel   = new JButton("Cancel");
		usernme  = new JTextField();
		pass     = new JPasswordField();

		 
		mysqluser="root";
		 mysqlpass="p@ssword";
		 mysqlhost="localhost";
		 port=3306;
		 
		 
		box.addActionListener(
				 new ActionListener(){
						
						public void actionPerformed(ActionEvent event){
							int index = box.getSelectedIndex();
							userType=modes[index]; 
						}
					}		
				
		);
		usernme.addActionListener(
				
				 new ActionListener(){
						
						public void actionPerformed(ActionEvent event){
						user=usernme.getText(); 
						}
					}		
				
		);
		
		pass.addActionListener(
				 new ActionListener(){
						
						@SuppressWarnings("deprecation") 
						public void actionPerformed(ActionEvent event){
						passw=pass.getText(); 
						}
					}		
				
		);
		cancel.addActionListener(
				
				 new ActionListener(){
						
						public void actionPerformed(ActionEvent event){
							if(cancel.isEnabled())
								System.exit(0);
						}
					}		
		
		);
		
		mode.setAlignmentX(JComponent.LEFT_ALIGNMENT);
		username.setAlignmentX(JComponent.LEFT_ALIGNMENT);
		password.setAlignmentX(JComponent.LEFT_ALIGNMENT);
		box.setAlignmentX(JComponent.LEFT_ALIGNMENT);
		ok.setAlignmentX(JComponent.LEFT_ALIGNMENT);
		usernme.setAlignmentX(JComponent.LEFT_ALIGNMENT);
		pass.setAlignmentX(JComponent.LEFT_ALIGNMENT);
		cancel.setAlignmentX(JComponent.LEFT_ALIGNMENT);
		 
		modePanel.add(mode);
		modePanel.add(box);
		this.add(modePanel);
		userPanel.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 7));//creates a space before the username :)
		userPanel.add(username);
		userPanel.add(usernme);
		this.add(userPanel);
		passPanel.add(password);
		passPanel.add(pass); 
		this.add(passPanel);
	    advPanel.setBorder(BorderFactory.createEmptyBorder(4, 5, 5, 5));//creates a space before the adv :)
		advPanel.add(adv);
		this.add(advPanel);
		okPanel.add(ok);
		okPanel.add(Box.createRigidArea(new Dimension(50, 0)));
		       
		okPanel.add(cancel);
		this.add(okPanel);
		Timer time = new Timer(5, this);
		time.start();
		
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		repaint();
		if(logframe){
			 mysqluser=log.login.getUser();
			 mysqlpass=log.login.getPass();
			 mysqlhost=log.login.getHost();
			 port=log.login.getPort();
		}
			
	}
	public void paint (Graphics g){
		
		super.paint(g);
		Graphics2D g2D = (Graphics2D) g;
		 
		String adv = "Advanced Settings";
		g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		    g2D.setColor(Color.BLUE);
			Font plainFont = new Font(null, Font.ITALIC, 30);
			Font plainFont2 = new Font(null, Font.PLAIN, 30);

		    AttributedString as = new AttributedString(adv);
		   
			as.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON, 0, adv.length());
			   
		    if(advanced.linkClicked)
			 as.addAttribute(TextAttribute.FONT, plainFont);
		    if(advanced.linkEntered)
		    	 as.addAttribute(TextAttribute.FONT, plainFont2);
			   	  
		      
		    g2D.drawString(as.getIterator(), 100, 102);
		 
	}
  
	public class ML implements MouseListener, KeyListener{

		@Override
		public void keyPressed(KeyEvent e) {
			// TODO Auto-generated method stub
			if(e.getKeyCode() == e.VK_ESCAPE)
			 System.exit(0);
		}

		@Override
		public void keyReleased(KeyEvent e) {
			// TODO Auto-generated method stub
			 
		}

		@Override
		public void keyTyped(KeyEvent e) {
			// TODO Auto-generated method stub
			if(e.getKeyCode() == e.VK_ESCAPE)
				 System.exit(0);
		}

		@Override
		public void mouseClicked(MouseEvent e) {
			// TODO Auto-generated method stub
			if(advanced.checkBounds(e.getX(), e.getY())){
				
				advanced.linkClicked = true; 
				logframe = true;
				log = new LogFrame(mysqluser, mysqlpass, mysqlhost, port);
				
			}
			System.out.print(mysqluser+mysqlpass +mysqlhost + port);
			advanced.linkEntered = true;
		}

		@Override
		public void mouseEntered(MouseEvent e) {
			// TODO Auto-generated method stub
			 
			
		}

		@Override
		public void mouseExited(MouseEvent e) {
			// TODO Auto-generated method stub
			 
		}

		@Override
		public void mousePressed(MouseEvent e) {
			// TODO Auto-generated method stub
			if(advanced.checkBounds(e.getX(), e.getY())){
				advanced.linkClicked = true; 
	 		}
			
		}

		@Override
		public void mouseReleased(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}
	}
	
}
